<template>
  <a-form :form="form" v-show="isFormDisable">
    <template  v-for="(po,index) in columns">
      <a-form-item :label="po.dbFieldTxt" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <j-date v-if="po.fieldShowType =='date'" :placeholder="'请选择'+po.dbFieldTxt" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :trigger-change="true" style="width: 100%"/>
        <j-date v-else-if="po.fieldShowType =='datetime'" :placeholder="'请选择'+po.dbFieldTxt" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        <j-popup v-else-if="po.fieldShowType =='popup'"
                 v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]"
                 :trigger-change="true"
                 :org-fields="po.dictField"
                 :dest-fields="po.dbFieldName"
                 :code="po.dictTable"
                 :field="po.dbFieldName"
                 @callback="popupCallback"/>

        <j-search-select-tag v-else-if="po.fieldShowType =='sel_search'" :placeholder="'请选择'+po.dbFieldTxt" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :dict="po.dictTable+','+po.dictText+','+po.dictField+','+po.condition"/>
        <j-category-select v-else-if="po.fieldShowType =='cat_tree'" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :pcode="po.oldDictTable" @change="handleCategoryChange"/>
        <j-select-depart v-else-if="po.fieldShowType =='sel_depart'" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :trigger-change="true"/>
        <j-select-user-by-dep v-else-if="po.fieldShowType =='sel_user'" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :trigger-change="true"/>
        <a-textarea v-else-if="po.fieldShowType =='textarea'" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" rows="4" :placeholder="'请输入'+po.dbFieldTxt"/>
        <j-dict-select-tag v-else-if="po.fieldShowType=='list' || po.fieldShowType=='radio'" :type="po.fieldShowType" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]"  :trigger-change="true" :dictCode="po.dictTable+','+po.dictText+','+po.dictField+','+po.condition" :placeholder="'请选择'+po.dbFieldTxt"/>
        <j-multi-select-tag v-else-if="po.fieldShowType=='list_multi' || po.fieldShowType=='checkbox'"  :type="po.fieldShowType" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :trigger-change="true" :dictCode="po.dictTable+','+po.dictText+','+po.dictField+','+po.condition" :placeholder="'请选择'+po.dbFieldTxt"/>
        <a-input-number v-else-if="po.fieldShowType=='int' || po.fieldShowType=='double' || po.fieldShowType=='BigDecimal'" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :placeholder="'请输入'+po.dbFieldTxt" style="width: 100%"/>
        <j-upload v-else-if="po.fieldShowType=='file'" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :trigger-change="true"></j-upload>
        <j-image-upload isMultiple v-else-if="po.fieldShowType=='image'" v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" ></j-image-upload>
        <j-editor v-else-if="po.fieldShowType =='umeditor'" v-decorator="[''+po.dbFieldName,{trigger:'input'}]"/>
        <a-input v-else  v-decorator="[''+po.dbFieldName, validatorRules[po.dbFieldName]]" :placeholder="'请输入'+po.dbFieldTxt"></a-input>
      </a-form-item>
    </template>
  </a-form>
</template>

<script>
  import pick from 'lodash.pick'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
  
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
  import JTreeSelect from '@/components/jeecg/JTreeSelect'
  import JPopup from '@comp/jeecg/JPopup'
  export default {
    name: 'OufuformInnerChildModal',
    components: {
      JDate,
      JUpload,
      JImageUpload,
      JSelectDepart,
      JSelectUserByDep,
      JDictSelectTag,
      JMultiSelectTag,
      JTreeSelect,
      JPopup,
      JSearchSelectTag
    },
    props: {
      code: '',
      parentCode: '',
      title:'',
      formColumns: {
        type: Array,
        default: null,
      }
    },
    data() {
      return {
        formTemplateMobile: 12,
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        confirmLoading: false,
        validatorRules: {},//待处理
        expandedRowKeys: [],
        currentTableName:'',
        columns:[],
        isFormDisable:false,
      }
    },
    created() {
      this.columns = this.formColumns;
      for(let a=0;a<this.formColumns.length;a++){
        this.formColumns[a]
        let po = this.formColumns[a];
        //处理验证
        if(po.dbFieldName != 'id'){
          let rules = [];
          if(po.fieldMustInput ==1 || po.fieldValidType =='*'){
            rules.push({ required: true, message: '请输入'+po.dbFieldTxt})
          }else if(po.fieldValidType=='only'){
            rules.push({ validator: (rule, value, callback) => validateDuplicateValue(this.currentTableName, po.dbFieldName, value, this.model.id, callback)})
          }else if(po.fieldValidType=='n6-16'){
            rules.push({ pattern: /\d{6,16}/, message: '请输入6到16位数字!'})
          }else if(po.fieldValidType=='*6-16'){
            rules.push({ pattern: /^.{6,16}$/, message: '请输入6到16位任意字符!'})
          }else if(po.fieldValidType=='s6-18'){
            rules.push({ pattern: /^.{6,18}$/, message: '请输入6到18位任意字符!'})
          }else if(po.fieldValidType=='url'){
            rules.push({ pattern: /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/, message: '请输入正确的网址!'})
          }else if(po.fieldValidType=='e'){
            rules.push({ pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'})
          }else if(po.fieldValidType=='m'){
            rules.push({ pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'})
          }else if(po.fieldValidType=='p'){
            rules.push({ pattern: /^[1-9]\d{5}$/, message: '请输入正确的邮政编码!'})
          }else if(po.fieldValidType=='s'){
            rules.push({ pattern: /^[A-Z|a-z]+$/, message: '请输入字母!'})
          }else if(po.fieldValidType=='n'){
            rules.push({ pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'})
          }else if(po.fieldValidType=='z'){
            rules.push({ pattern: /^-?\d+$/, message: '请输入整数!'})
          }else if(po.fieldValidType=='money'){
            rules.push({ pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'})
          }
          this.validatorRules[po.dbFieldName] = {rules:rules};
        }
      }
      this.isFormDisable = true;
    },
    methods: {
      initFormData(url,id){
        this.clearFormData()
        if(!id){
          this.edit({})
        }else{
          let params = {};
          params['autoHeadId'] = this.code;
          params['autoParentHeadId'] = this.parentCode;
          params['id'] = id;
          getAction(url,params).then(res=>{
            if(res.success){
              let records = res.result
              if(records && records.length>0){
                this.edit(records[0])
              }
            }
          })
        }
      },
      edit(record){
        this.model = Object.assign({}, record)
        this.$nextTick(() => {
          this.columns.forEach(item =>{
            this.form.setFieldsValue(
              pick(this.model,item.dbFieldName),
            )
          })
        })
      },
      getFormData(){
        let formdata_arr = []
        this.form.validateFields((err, values) => {
          if (!err) {
            let formdata = Object.assign(this.model, values)
            let isNullObj = true
            Object.keys(formdata).forEach(key=>{
              if(formdata[key]){
                isNullObj = false
              }
            })
            if(!isNullObj){
              formdata_arr.push(formdata)
            }
          }else{
            this.$emit("validateError",this.title+"表单校验未通过");
          }
        })
        return formdata_arr;
      },
      clearFormData(){
        this.form.resetFields()
        this.model={}
      },
      popupCallback(row) {
        for(var key in row){
          this.model[key] = row[key]
          let fieldval = pick(this.model, key)
          this.$nextTick(() => {
            this.form.setFieldsValue(fieldval)
          })
        }
      },
      handleCategoryChange(value,backObj){
        this.form.setFieldsValue(backObj)
      }
    },
  }
</script>
